@echo off
setlocal enabledelayedexpansion

REM 设置python命令名称
set "python=python3"

REM 检查 Python 版本是否在 3.8 或更高版本
%python% -c "import sys; assert(sys.version_info >= (3,8))" 2> nul

REM 如果返回值不为零，即 Python 版本不满足要求，则输出错误消息并退出脚本
if %errorlevel% neq 0 (
    echo "Python 3.8 or higher is required."
    exit /b 1
)

set /p "env_path=Please enter the python virtual environment path(eg:C:\Users\yourname\Envs\env_mcu_tools):"
set "env_path=%env_path:/=\%"

REM 判断输入的路径不为空
if "%env_path%" equ "" (
    echo [ERROR]The path is not set, exit
    exit /b 1
)

REM 检查路径下虚拟环境是否存在
if not exist "%env_path%\Scripts\activate" (
    set /p create_env="The virtual environment is not found. Determine whether to create a virtual environment(y/n):"
    if /i "!create_env!"=="y" (
        echo [INFO]Creating a virtual environment...
        %python% -m venv %env_path% || (
            echo [ERROR]Unable to create virtual environment. Please check the input path.
            pause 
            exit /b 1
        )
    ) else (
        echo [ERROR]Exit
        exit /b 1
    )
)

set "python_exe=%env_path%\Scripts\python.exe"
set "pip_exe=%env_path%\Scripts\pip.exe" 
set "req_txt=%cd%\requirements.txt"

REM 设置pip国内源
set "pip_args=--index-url https://pypi.tuna.tsinghua.edu.cn/simple"


call "%env_path%\Scripts\activate"
set "pip_version=" 
for /f "tokens=2 delims= " %%i in ('%pip_exe% --version') do set "pip_version=%%i"

REM 升级虚拟环境下pip版本，升级失败后退出
%python_exe% -m pip install --upgrade pip %pip_args%
if not errorlevel 1 ( 
    echo [INFO]Pip has been upgraded to the latest version.
) else ( 
    echo [WARN]Pip upgrade failed, exit.
    exit /b 1
)

REM 检查当前路径下requirements.txt是否存在
if exist "%req_txt%" (
    echo [INFO]Installing dependent libraries... 
    set req_error=0
    REM 遍历文件内的行，以‘==’作为分隔符，取前后两块数据
    for /f "tokens=1,2 delims==" %%i in ('type %req_txt%') do (
        set "dependency_name=%%i"
        set "version=%%j"
        echo Dependency name: !dependency_name!,version: !version!
        REM 安装指定版本依赖
        %python_exe% -m pip install !dependency_name!==!version!
        if not errorlevel 1 (
            echo [INFO]%%i Installation succeeded.
        ) else (
            REM 安装失败后使用默认安装指令重新安装
            echo [WARN]There was an error installing the %%i, trying to install the default version.
            %pip_exe% install %%i %pip_args%
            if not errorlevel 1 (
                echo [INFO]The default version of the %default_version% was installed successfully
            ) else (
                echo [ERROR]Error installing the default version, please reinstall it manually.
                set req_error=1
            )
        )
    )
    REM 检查所有依赖是否都安装完成
    if !req_error! equ 0 (
        echo [INFO]All dependent libraries were installed successfully.
    ) else ( 
        echo [ERROR]Dependent library installation failed, please reinstall it manually. 
        pause 
        exit /b 1
    )
) else ( 
    echo [INFO]The requirements.txt file was not found. Make sure it is in the same path as the script.
    pause
    exit /b 1 
)
